import os
import shutil


def rename_and_move_files(root_dir, output_dir):
    for root, dirs, files in os.walk(root_dir):
        for file_name in files:
            # 构建新文件名
            new_file_name = os.path.relpath(root, root_dir).replace(os.path.sep, "_") + "_" + file_name
            old_file_path = os.path.join(root, file_name)
            new_file_path = os.path.join(output_dir, new_file_name)

            # 创建文件副本
            shutil.copy2(old_file_path, new_file_path)

            # 重命名文件
            os.rename(new_file_path, os.path.join(output_dir, new_file_name))


# 源路径A和目标路径B
root_dir = "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/给预备役/json_annotations (copy)/good (copy)"
output_dir = "/home/champrin/Desktop/MV-CS016-10UC+DA1041860/给预备役/json_annotations (copy)/good (copy) rename"
os.makedirs(output_dir, exist_ok=True)

# 重命名并移动文件
rename_and_move_files(root_dir, output_dir)